
var estimateTemplate_add = new estimateTemplate_add();
function estimateTemplate_add() {
    var _this = this;
    var page;
    _this.index = -1;
    var items;
    _this.init = function (page_person) {
        page = page_person;
        _this.initTable();
    };

    _this.initTable = function () {
        var list_url = appPath + "/admin/estimateQuestion/page";
        page.init("estimateTemplate_add_form", list_url, "template_question_table", "template_page", 1, 1000);
        page.goPage(1);
        page.list = function (dataList) {
            // $("#checkAll").attr("checked", false);
            var len = dataList.length;
            items = dataList;
            var inner = "", item;
            // 组装数据
            for (var i = 0; i < len; i++) {
                item = dataList[i];
                inner += '<tr>';
                inner += '<td width="10%"><span>' + item.sort + '</span></td>';
                inner += '<td width="50%"><span>' + item.content.title + '</span></td>';
                inner += '<td width="10%"><span>' + parseChrType(item.chrType) + '</span></td>';
                inner += '<td width="30%"><a href="javascript:estimateTemplate_add.modify(\'' + item.id + '\');" id="btn_all" class="a a-view">修改</a> ';
                inner += '<a href="javascript:estimateTemplate_add.delete(\'' + item.id + '\');"  id="btn_edit" class="a a-view">删除</a>';

                if (i != 0) {
                    inner += '<a href="javascript:estimateTemplate_add.moveUp(\'' + item.id + '\');"  id="btn_moveUp" class="a a-view">上移</a>';
                }
                if (i != len-1) {
                    inner += '<a href="javascript:estimateTemplate_add.moveDown(\'' + item.id + '\');"  id="btn_moveDown" class="a a-view">下移</a>';
                }

                inner += '</td></tr>';
            }
            return inner;
        }
    };

    _this.search = function () {
        _this.initTable();
    };

    //保存
    $("#save").click(function () {
        var estimateName = $.trim($("#estimateName").val())
        if ("" == estimateName) {
            layer.msg("请填写效果评估模板名称");
            return;
        }

        var params = {
            "id": $("#id").val(),
            "estimateName": estimateName,
            "remark": $("#remark").val(),
            "validDate": $("#validDate").val()
        };

        $.ajax({
            url: appPath + "/admin/estimate/addSave",
            type: 'post',
            data: params,
            success: function (data) {
                if ("10000" == data.code) {
                    layer.msg(data.message);
                    $("#id").val(data.result)
                } else {
                    layer.alert('操作失敗', {icon: 2, skin: 'layer-ext-moon'});
                }
            }
        });
    });

    // 上移
    _this.moveUp = function (questionId) {

        _this.index = layer.alert('是否确定上移', {
            btn: ['确定', '取消'],
            btn1: function () {
                $.ajax({
                    url: appPath + "/admin/estimateQuestion/moveUpAndDown",
                    data: {questionId: questionId,moveFlag:'up'},
                    dataType: "json",
                    async: true,
                    type: "POST",
                    success: function (data) {
                        var code = eval(data).code;
                        var result = eval(data).result;
                        if ("10000" == code) {
                            layer.msg("上移成功");
                            _this.initTable();
                        } else {
                            var tipMsg = statusTxt + "失败!";
                            var error = eval(data).errorMsg;
                            if(error != null && error != "") {
                                tipMsg += "\n" + error;
                            }

                        }
                    }
                });
            }
        })

    };

    // 下移
    _this.moveDown = function (questionId) {
        _this.index = layer.alert('是否确定下移', {
            btn: ['确定', '取消'],
            btn1: function () {
                $.ajax({
                    url: appPath + "/admin/estimateQuestion/moveUpAndDown",
                    data: {questionId: questionId,moveFlag:'down'},
                    dataType: "json",
                    async: true,
                    type: "POST",
                    success: function (data) {
                        var code = eval(data).code;
                        var result = eval(data).result;
                        if ("10000" == code) {
                            layer.msg("下移成功");
                            _this.initTable();
                        } else {
                            var tipMsg = statusTxt + "失败!";
                            var error = eval(data).errorMsg;
                            if(error != null && error != "") {
                                tipMsg += "\n" + error;
                            }

                        }
                    }
                });
            }
        })
    };

    // 修改
    _this.modify = function (questionId) {
        layer.open({
            type: 2,
            title: '修改问题',
            area: ['1200px', '90%'],		//弹出层大小
            // scrollbar: true,				//false隐藏滑动块
            content: [appPath + '/admin/estimateQuestion/update?questionId='+questionId, 'yes']
        });
    }

    // 删除
    _this.delete = function (questionId) {

        _this.index = layer.alert('是否确定删除', {
            btn: ['确定', '取消'],
            btn1: function () {
                $.ajax({
                    url: appPath + "/admin/estimateQuestion/delete",
                    data: {questionId: questionId},
                    dataType: "json",
                    async: true,
                    type: "POST",
                    success: function (data) {
                        var code = eval(data).code;
                        var result = eval(data).result;
                        if ("10000" == code) {
                            layer.msg("删除成功");
                            _this.initTable();
                        } else {
                            var tipMsg = statusTxt + "失败!";
                            var error = eval(data).errorMsg;
                            if(error != null && error != "") {
                                tipMsg += "\n" + error;
                            }

                        }
                    }
                });
            }
        })
    }

    //弹窗-新增问题
    $("#add_question").click(function () {

        var id = $("#id").val();
        if (id == '' || id == undefined || id == null) {
            layer.alert("请先添加模板");
            return
        }

        layer.open({
            type: 2,
            title: '新增问题',
            area: ['1200px', '80%'],		//弹出层大小
            scrollbar: false,				//false隐藏滑动块
            content: [appPath + '/admin/estimateQuestion?estimateId='+id, 'yes']
        });

    });

}

function parseChrType(str) {

    if (str == '01') {
        return "单选"
    } else if (str == '02') {
        return "多选"
    } else if (str == '03') {
        return "判断"
    }else if (str == '05') {
        return "简答"
    }

    return str;
}
